package com.ys.erp.stage_wfb_service.config;

import com.alipay.v3.ApiClient;
import com.alipay.v3.ApiException;
import com.alipay.v3.util.model.AlipayConfig;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lwj
 * @date 2024/7/31
 */
@SpringBootConfiguration
public class ZfbConfig {

    public static ConcurrentHashMap<String, ApiClient> zfbClientMap = new ConcurrentHashMap<>();
    public static ConcurrentHashMap<String, String> zfbOriginAccountMap = new ConcurrentHashMap<>();
    public static ConcurrentHashMap<String, ApiClient> accountClientMap = new ConcurrentHashMap<>();

    public ZfbConfig() {
        zfbOriginAccountMap.put("hz0", "hz123cwmy@163.com");
        zfbOriginAccountMap.put("jx3", "jx2yscw@163.com");
        zfbOriginAccountMap.put("sh1", "SH1yscw@163.com");
    }

    @Bean
    public ApiClient getClient() {
        ApiClient defaultClient = new ApiClient();
        // 初始化alipay参数（全局设置一次）
        try {
            defaultClient.setAlipayConfig(getAlipayConfig());
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        zfbClientMap.put("hz0", defaultClient);
        accountClientMap.put("hz123cwmy@163.com", defaultClient);
        return defaultClient;
    }

    @Bean("jx3ApiClient")
    public ApiClient getJx3Client() {
        ApiClient defaultClient = new ApiClient();
        // 初始化alipay参数（全局设置一次）
        try {
            defaultClient.setAlipayConfig(getjx3AlipayConfig());
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        zfbClientMap.put("jx3", defaultClient);
        accountClientMap.put("jx2yscw@163.com", defaultClient);
        return defaultClient;
    }

    @Bean("sh1ApiClient")
    public ApiClient getSh1Client() {
        ApiClient defaultClient = new ApiClient();
        // 初始化alipay参数（全局设置一次）
        try {
            defaultClient.setAlipayConfig(getSh1AlipayConfig());
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
        zfbClientMap.put("sh1", defaultClient);
        accountClientMap.put("SH1yscw@163.com", defaultClient);
        return defaultClient;
    }

    private static AlipayConfig getAlipayConfig() {
        // 设置alipayConfig参数（全局设置一次）
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com");
        alipayConfig.setAppId("2021005158602505");
        alipayConfig.setPrivateKey(readPrivateKey("aliCert/appPrivateKey.pem"));

        ClassPathResource appCertResource = new ClassPathResource("aliCert/appCertPublicKey.crt");
        alipayConfig.setAppCertPath(appCertResource.getPath());

        ClassPathResource aliRootCertResource = new ClassPathResource("aliCert/alipayRootCert.crt");
        alipayConfig.setRootCertPath(aliRootCertResource.getPath());

        ClassPathResource aliCertResource = new ClassPathResource("aliCert/alipayCertPublicKey_RSA2.crt");
        alipayConfig.setAlipayPublicCertPath(aliCertResource.getPath());

        return alipayConfig;
    }

    private static AlipayConfig getjx3AlipayConfig() {
        // 设置alipayConfig参数（全局设置一次）
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com");
        alipayConfig.setAppId("2021005172699354");
        alipayConfig.setPrivateKey(readPrivateKey("aliCert/jx3/appPrivateKey.pem"));

        ClassPathResource appCertResource = new ClassPathResource("aliCert/jx3/appCertPublicKey.crt");
        alipayConfig.setAppCertPath(appCertResource.getPath());

        ClassPathResource aliRootCertResource = new ClassPathResource("aliCert/jx3/alipayRootCert.crt");
        alipayConfig.setRootCertPath(aliRootCertResource.getPath());

        ClassPathResource aliCertResource = new ClassPathResource("aliCert/jx3/alipayCertPublicKey_RSA2.crt");
        alipayConfig.setAlipayPublicCertPath(aliCertResource.getPath());

        return alipayConfig;
    }

    private static AlipayConfig getSh1AlipayConfig() {
        // 设置alipayConfig参数（全局设置一次）
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com");
        alipayConfig.setAppId("2021006100613972");
        alipayConfig.setPrivateKey(readPrivateKey("aliCert/sh1/appPrivateKey.pem"));

        ClassPathResource appCertResource = new ClassPathResource("aliCert/sh1/appCertPublicKey.crt");
        alipayConfig.setAppCertPath(appCertResource.getPath());

        ClassPathResource aliRootCertResource = new ClassPathResource("aliCert/sh1/alipayRootCert.crt");
        alipayConfig.setRootCertPath(aliRootCertResource.getPath());

        ClassPathResource aliCertResource = new ClassPathResource("aliCert/sh1/alipayCertPublicKey_RSA2.crt");
        alipayConfig.setAlipayPublicCertPath(aliCertResource.getPath());

        return alipayConfig;
    }

    /**
     * 读取应用私钥
     */
    private static String readPrivateKey(String path) {
        ClassPathResource resource = new ClassPathResource(path);
        StringBuilder sb = new StringBuilder();

        // 使用InputStream代替FileReader解决JAR内资源访问问题
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.startsWith("-----")) {
                    sb.append(line);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


}
